Ext.define("HRMobile.controller.SurveyController", {
    extend: "Ext.app.Controller",
    config: {
        refs: {
        	listSurvey: '#listSurvey'
        },
        control:{
        	listSurvey: {
        		itemtap : function(list, index, target, record){
        			var detail = Ext.create(Ext.form.Panel,{
        				title: record.data.name,
        				padding : 20,
						height: '100%',
        				
        			});
        			Ext.Ajax.request({
        	            url: 'SurveyDetailGetServlet',
        	            params: {
        	                id: record.data.id,
        	                
        	            },	
        	            success: function(response) {
        	            	var result = Ext.JSON.decode(response.responseText);
        	            	var i = 1;
        	            	for (var question in result.questions){
        	            		var item = result.questions[question];
        	            		var fieldset = new Ext.form.FieldSet();
        	            		fieldset.setTitle("Question "+ i);
        	            		fieldset.setPadding(15);
        	            		
        	            		//question label
        	            		var label = new Ext.Label();
        	            		label.setHtml('<div style ="font-size:0.8em;padding:0.4em">'+item.text+'</div>');
        	            		fieldset.add(label);
        	            		
        	            		//choices
        	            		if (item.type == 1){
        	            			//multi select
        	            			for (var answer in item.Answers){
        	            				var choice = item.Answers[answer];
        	            				var checkbox = new Ext.field.Checkbox();
        	            				checkbox.setValue(choice.id);
        	            				checkbox.setName(item.id);
        	            				checkbox.setLabel(choice.label +". " +choice.text);
        	            				checkbox.setLabelWrap(true);
        	            				checkbox.setLabelWidth("70%");
        	            				fieldset.add(checkbox);
        	            				if (choice.type == 2){
        	            					// free text
        	            					fieldset.add({
        	            						xtype: 'textareafield',
        	            						maxRows: 3,
                		                   		name: choice.id+'_freetext',
//                		                   		label: ' '
        	            							});	
        	            				}
        	            			}
        	            		}
        	            		else {
        	            			//single select
        	            			for (var answer in item.Answers){
        	            				var choice = item.Answers[answer];
        	            				var radio = new Ext.field.Radio();
        	            				radio.setName(item.id);
        	            				radio.setLabel(choice.label +". " +choice.text);
        	            				radio.setLabelWrap(true);
        	            				radio.setLabelWidth("70%");
        	            				radio.setValue(choice.id);
        	            				fieldset.add(radio);
        	            				if (choice.type == 2){
        	            					// free text
        	            					fieldset.add({
        	            						xtype: 'textareafield',
        	            						maxRows: 3,
                		                   		name: choice.id+'_freetext',
//                		                   		label: ' '
        	            							});	
        	            				}
        	            			}
        	            		}
        	            		detail.add(fieldset);
        	            		i = i + 1;
        	            	};
        	            	detail.add({
						xtype : 'button',
						ui : 'action',
						width : 150,
						margin: '15px auto',
						text : 'Submit',
						handler : function() {
							var answered = 0;
							var values = detail.getValues();
							for(var key in values) {
								if (key.indexOf("_freetext") == -1){
								var value = values[key];
									if (value != null && value != ''){
										answered = answered + 1;
									}
								}
							}
							var question = result.questions.length;
							if (answered < question)
								{
								Ext.Msg.alert("Alert","Please answer all the question");
								return;
								}
							
							detail.submit({
							    url: 'SurveyDo',
							    method: 'POST',
							    params: {
					                topicId: record.data.id,
					                userId: localStorage.userIdSurvey
					            },
							    success: function() {
							    	Ext.Msg.alert("Alert","Thank you for taking the survey",function(){
							    		Ext.getCmp('nvSurvey').pop();
							    		Ext.getStore('SurveyStore').load();
							    	});
							    }
							});
						}

					});
        	            }
        	        });
        			Ext.getCmp('nvSurvey').push(detail);
                },
            },
        }
      },
});
